package com.zeei.bfs.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageInfo;
import com.zeei.bfs.dao.ConfigurationManagementDao;
import com.zeei.bfs.service.ConfigurationManagementService;
import com.zeei.bfs.vo.GeneralCategory;
import com.zeei.bfs.vo.RoleButtonVO;
import com.zeei.bfs.vo.SystemLog;
import com.zeei.bfs.vo.UserButtonVO;
import com.zeei.common.constant.GlobalConstant;
import com.zeei.common.utils.PropertiesUtil;
import com.zeei.common.vo.PointSysTypeVO;

@Service("configurationManagementService")
public class ConfigurationManagementServiceImpl extends BasicServiceImpl implements ConfigurationManagementService {
	@Autowired
	private ConfigurationManagementDao configurationManagementDao;

	@Override
	public PageInfo<GeneralCategory> dictionaryManagement(GeneralCategory generalCategoryParam) {
		this.setPage(generalCategoryParam.getPage(), generalCategoryParam.getRows());
		List<GeneralCategory> list = configurationManagementDao.queryDictionaryMessage(generalCategoryParam);
		return new PageInfo<>(list);
	}

	@Override
	public PageInfo<SystemLog> systemLogManagement(SystemLog systemLogparam) {

		this.setPage(systemLogparam.getPage(), systemLogparam.getRows());
		List<SystemLog> list = configurationManagementDao.querySystemLogMessage(systemLogparam);
		return new PageInfo<>(list);
	}

	@Override
	public boolean editDictionary(GeneralCategory generalCategoryParam, String oldGeneralCode) {
		if (StringUtils.isEmpty(oldGeneralCode)) {
			Integer count = configurationManagementDao.updateDictionaryByGeneralCode(generalCategoryParam);
			return count > 0;
		} else {
			Integer count = configurationManagementDao.dropDictionary(oldGeneralCode);
			count += configurationManagementDao.addDictionary(generalCategoryParam);
			return count > 1;
		}
	}

	@Override
	public boolean addDictionary(GeneralCategory generalCategoryParam) {

		Integer count = configurationManagementDao.addDictionary(generalCategoryParam);
		return count > 0;
	}

	@Override
	public boolean dropDictionary(String generalCode) {

		Integer count = configurationManagementDao.dropDictionary(generalCode);
		return count > 0;
	}

	@Override
	public List<GeneralCategory> getCategoryClass() {
		return configurationManagementDao.getCategoryClass(new GeneralCategory());
	}

	@Override
	public List<GeneralCategory> getCategoryList(String categoryclass) {
		return configurationManagementDao.getCategoryList(categoryclass);
	}

	@Override
	public boolean validGeneralCode(String generalCode) {
		if (StringUtils.isEmpty(generalCode)) {
			return GlobalConstant.FAILED;// 失败
		}
		Map<String, Object> condition = new HashMap<>();
		condition.put("generalCode", generalCode);
		List<GeneralCategory> list = configurationManagementDao.queryDictionaryMessageByCondition(condition);
		if (list.isEmpty()) {
			return GlobalConstant.SUCCEED;// 成功
		}
		return GlobalConstant.FAILED;// 失败
	}

	@Override
	public boolean validCategoryClass(String categoryClass) {

		if (StringUtils.isEmpty(categoryClass)) {
			return GlobalConstant.FAILED;// 失败
		}
		GeneralCategory generalCategoryParam = new GeneralCategory();
		generalCategoryParam.setCategoryClass(categoryClass);
		List<GeneralCategory> list = configurationManagementDao.getCategoryClass(generalCategoryParam);
		if (CollectionUtils.isEmpty(list)) {
			return GlobalConstant.SUCCEED;// 成功
		}
		return GlobalConstant.FAILED;// 失败

	}

	@Override
	public boolean validCategoryName(String categoryName) {

		if (StringUtils.isEmpty(categoryName)) {
			return GlobalConstant.FAILED;// 失败
		}
		GeneralCategory generalCategoryParam = new GeneralCategory();
		generalCategoryParam.setCategoryName(categoryName);
		List<GeneralCategory> list = configurationManagementDao.getCategoryName(generalCategoryParam);
		if (CollectionUtils.isEmpty(list)) {
			return GlobalConstant.SUCCEED;// 成功
		}
		return GlobalConstant.FAILED;// 失败

	}

	@Override
	public boolean addDictionaryType(GeneralCategory generalCategoryParam) {
		Integer count = configurationManagementDao.addDictionaryType(generalCategoryParam);
		return count > 0;
	}

	@Override
	public boolean dropDictionary(List<String> list) {
		Integer count = configurationManagementDao.dropDictionaryList(list);
		return count > 0;
	}

	@Override
	public List<RoleButtonVO> getRoleButton() {
		return configurationManagementDao.getRoleButton();
	}

	@Override
	public List<UserButtonVO> getUserButton() {
		return configurationManagementDao.getUserButton();
	}

	@Override
	public List<GeneralCategory> getCategoryListBySystemType(String categoryClass, String systemType) {
		Map<String, Object> condition = new HashMap<String, Object>();
		condition.put("systemType", systemType);
		condition.put("categoryClass", categoryClass);
		return configurationManagementDao.getCategoryListBySystemType(condition);
	}

	@Override
	public List<String> getExistsTable(List<String> tableNames) {
		Map<String, Object> condition = new HashMap<String, Object>();
		condition.put("tableNames", tableNames);

		// 获取配置的数据库名称
		String dataBaseName = "";
		Boolean mysqlDataBaseName = true;

		String dataBaseUrl = PropertiesUtil.getProperty(PropertiesUtil.loadProperties("config.properties"), "jdbc.url");
		String regex = "/.+\\?";
		Pattern p = Pattern.compile(regex);
		Matcher m = p.matcher(dataBaseUrl);
		if (m.find()) {
			mysqlDataBaseName = false;
			dataBaseName += m.group();
			dataBaseName = dataBaseName.substring(dataBaseName.lastIndexOf("/") + 1, dataBaseName.length() - 1);
		}

		if (mysqlDataBaseName) {
			dataBaseName = dataBaseUrl.substring(dataBaseUrl.lastIndexOf("=") + 1);
		}
		// String systemTableName =
		// configurationManagementDao.getSystemTableByDataType(condition);
		// condition.put("systemTableName", systemTableName);

		condition.put("dataBaseName", "'" + dataBaseName + "'");
		return configurationManagementDao.getExistsTable(condition);
	}

	@Override
	public List<PointSysTypeVO> getSystemTable(int systemType) {
		Map<String, Object> condition = new HashMap<String, Object>();
		condition.put("systemType", systemType);
		List<PointSysTypeVO> systemTableName = configurationManagementDao.getSystemTable(condition);
		return systemTableName;
	}

	@Override
	public List<GeneralCategory> queryCategoryClassBySystemType(Map<String, Object> condition) {
		return configurationManagementDao.queryCategoryClassBySystemType(condition);
	}
}
